package com.tarena.lbs.basic.web.service.impl;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.base.protocol.pager.PageResult;


import com.tarena.lbs.basic.web.repository.BusinessRepository;
import com.tarena.lbs.basic.web.repository.DictItemRepository;
import com.tarena.lbs.basic.web.service.IAdminService;
import com.tarena.lbs.basic.web.service.IBusinessService;
import com.tarena.lbs.common.attach.enums.BusinessTypeEnum;
import com.tarena.lbs.common.basic.converter.BusinessConverter;
import com.tarena.lbs.common.basic.converter.StoreConverter;
import com.tarena.lbs.common.basic.enums.BusStatusEnum;
import com.tarena.lbs.pojo.attach.dto.AttachDTO;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.bo.BusinessBO;
import com.tarena.lbs.pojo.basic.bo.DictItemBO;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.dos.BusinessDO;
import com.tarena.lbs.pojo.basic.param.BusinessParam;
import com.tarena.lbs.pojo.basic.query.BusinessQuery;
import com.tarena.lbs.pojo.basic.vo.BusiStoreVO;
import com.tarena.lbs.pojo.basic.vo.BusinessVO;
import com.tarena.lbs.pojo.basic.vo.StoreVO;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
public class BusinessServiceImpl implements IBusinessService {


    @Autowired
    private BusinessRepository businessRepository;

    @Autowired
    private DictItemRepository dictItemRepository;

    @Autowired
    private StoreConverter storeConverter;

    @Autowired
    private IAdminService adminService;

    @Autowired
    private BusinessConverter businessConverter;

    @Autowired
    private AttachApi attachApi;

    @Value("${app.attach.url}")
    private String url;

    @Override
    public void saveParam(BusinessParam param) {
        BusinessDO businessDO = new BusinessDO();
        //行业状态为字典数据详情表的id
        BeanUtils.copyProperties(param, businessDO);
        Date date = new Date();
        //设置商家入驻时间
        businessDO.setEntryTime(date);
        //新增时，设置商家的状态
        businessDO.setBusinessStatus(BusStatusEnum.PENDING.getCode());
        businessRepository.insert(businessDO);
    }

    @Override
    public BusiStoreVO detail(Integer id) {
        BusinessBO businessBO = businessRepository.detail(id);
        if (businessBO==null){
            return null;
        }
        DictItemBO detail = dictItemRepository.detail(businessBO.getIndustryType());
        BusiStoreVO busiStoreVO = new BusiStoreVO();
        BeanUtils.copyProperties(businessBO, busiStoreVO);
        busiStoreVO.setIndustryName(detail == null ? "" :detail.getDictValue());
        List<StoreBO> list = businessRepository.selectByBuIdcon(id);
        if (!CollectionUtils.isEmpty(list)) {
            List<Integer> storeIdList = list.stream().map(e -> e.getId()).collect(Collectors.toList());
            List<AttachDTO> attachDTOList = attachApi.batchGetAttachInfo(storeIdList, BusinessTypeEnum.SHOP_LOGO.getType());
            Map<Integer, List<AttachDTO>> dataMap = attachDTOList.stream()
                .collect(Collectors.groupingBy(AttachDTO::getBusinessId));
            for (StoreBO storeBO : list) {
                if (dataMap != null && dataMap.get(storeBO.getId()) != null) {
                    storeBO.setStoreLogoPic(url + dataMap.get(storeBO.getId()).get(0).getFileUuid());
                }
            }
            List<StoreVO> storeVOS = storeConverter.boList2VOList(list);
            busiStoreVO.setStoreVOList(storeVOS);
        }
        return busiStoreVO;
    }

    @Override
    public BusinessVO getBusinessInfoByAdminId(Integer adminId) {
        AdminBO adminBO = adminService.getAdminById(adminId);
        BusinessBO businessBO = businessRepository.detail(adminBO.getBusinessId());
        return businessConverter.bo2vo(businessBO);
    }

    @Override
    public BusinessVO businessDetail(Integer id) {
        BusinessBO businessBO = businessRepository.detail(id);
        return businessConverter.bo2vo(businessBO);
    }

    @Override
    public void update(Integer id, BusinessParam businessParam) {
        BusinessDO businessDO = new BusinessDO();
        BeanUtils.copyProperties(businessParam, businessDO);
        businessDO.setId(id);
        businessRepository.update(businessDO);
    }

    @Override
    public void updateStatus(BusinessParam businessParam) {
        BusinessDO businessDO = new BusinessDO();
        BeanUtils.copyProperties(businessParam, businessDO);
        businessRepository.update(businessDO);
    }

    @Override
    public PageResult<BusinessVO> getList(BusinessQuery businessQuery) {
        int pageNo = businessQuery.getPageNo();
        int pageSize = businessQuery.getPageSize();
        //new PageQuery(pageNo, pageSize);
        List<BusinessDO> boList = businessRepository.selectList(businessQuery);
        PageInfo<BusinessDO> pageInfo = new PageInfo<>(boList);
        List<BusinessVO> businessVOList = pageInfo.getList().stream().map(businessDO -> {
            BusinessVO businessVO = new BusinessVO();
            if (businessDO!=null){
                DictItemBO detail = dictItemRepository.detail(businessDO.getIndustryType());
                BeanUtils.copyProperties(businessDO, businessVO);
                businessVO.setIndustryName(detail == null ? "" :detail.getDictValue());
            }
            return businessVO;
        }).collect(Collectors.toList());

        PageResult<BusinessVO> pageResult = new PageResult<>();
        pageResult.setObjects(businessVOList);
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setPageNo(pageNo);
        pageResult.setPageSize(pageSize);
        return pageResult;

    }

    @Override
    public  List<BusinessBO> getAll(Integer businessStatus) {
        List<BusinessBO> boList = businessRepository.selectAll(businessStatus);
        return boList;
    }
}
